package com.bluegalaxy.nhb.snservice.core.encryp;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Liu Xiongwei
 * Date: 2016/7/26
 * Time: 10:39
 * Description:
 * To change this template use File | Settings | File Templates.
 */
public class SNHelper {

    private Log log = LogFactory.getLog(SNHelper.class);


    /**
     * 将加密内容转换为对象
     * @param reqData
     * @param des3Key
     * @return
     */
    public static Object decodeStrToObject(String reqData, Class clazz, String des3Key){
        Object o = null;
        try {
            o = JSONObject.parseObject(decode(reqData,des3Key),clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    /**
     * 加密 Base64(desc3_ecb(str,key))
     * @param requestStr
     * @param des3key
     * @return
     * @throws Exception
     */
    public static String encode(String requestStr , String des3key ) throws Exception {
        byte[] tempByte = Des3Helper.des3EncodeECB(des3key.getBytes("UTF-8"), requestStr.getBytes("UTF-8"));
        String reqData = BASE64.encode(tempByte);
        return reqData;
    }

    /**
     * 返解
     * @param responseData
     * @return
     * @throws Exception
     */
    public static String decode(String responseData, String des3key) throws Exception {
        //反解BASE64
        byte[] decodeBase64 = BASE64.decode(responseData);
        //反解DES
        byte[] decodeDes = Des3Helper.des3DecodeECB(des3key.getBytes(), decodeBase64);

        String resJson = new String(decodeDes);

        return resJson;
    }

}
